package ua.model;


import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;

import ua.common.XmlManager;
import ua.model.TomcatUsersBean;

public abstract class RoleListBean {
	
	public static TomcatUsersBean getTomcatUser()
	{	
		TomcatUsersBean tomcatUsersBean = (TomcatUsersBean) XmlManager.getInstance().getObjectXml();
		tomcatUsersBean.updateRolesFromStr();
		return tomcatUsersBean;
	}
	
	public static List<RoleBean> getRoles(String key)
	{
		List<RoleBean> roleList = filter(getTomcatUser().getRoles(), key == null ? "" : key);
		return roleList;	
	}
	
	public static List<RoleBean> filter(List<RoleBean> listRole, String key) {
		List<RoleBean> newList = new LinkedList<RoleBean>();
		for (RoleBean role : listRole) {
			if (role.getRolename().startsWith(key)) {
				newList.add(role);
			}
		}
		return newList;
	}
	
	public static HashSet<RoleBean> getRoles() 
	{    
		HashSet<RoleBean> hashSet=new HashSet<RoleBean>(); 
		List<RoleBean> listRole = filter(getTomcatUser().getRoles(),"");
		for (RoleBean role : listRole) {
			hashSet.add(role);
		}
		return hashSet;
	}
	
	public static RoleBean getRoleByName(String role)
	{
		return getTomcatUser().getRoleByName(role);
	}
}
